class TimeSlider {

  private PImage handle;
  private  float x1;
  private   float y1;
  private   float timeBegin;
  private   float timeEnd ;
  private   float left;
  private   float right;
  private   float ix;
  private   float iy;
  private   float w;
  private   float h;
  private   float handleWidth;
  private   float handleHeight;
  private   int dragMode ;
  public int numOfBar;
  public float barHeight;
  private String barDataFile;
  private BufferedReader reader;
  private int [] barData;
  private int maxBar;
  private String [] barDes;
  /********************************************************************************
   *                                                                              *
   *                               Constructors                                   *
   *                                                                              *
   ********************************************************************************/
  TimeSlider(float x, float y, float wei, float hei)
  {
    x1 = x;
    y1 = y;
    w = wei;
    h = hei;
    loTimeSlider = new MyLocation(x,y,w,h);
    handle = loadImage("timeSliderHandle.png");
    timeBegin = 1973;
    timeEnd = 2010.5;

    numOfBar = int((timeEnd-timeBegin)*2 );
    left =0;
    right =numOfBar;
    ix = w/numOfBar;
    handleHeight = h;
    handleWidth = h;
    dragMode = 0 ;
    barHeight =30;
    barDataFile = "eq_yr_count.csv";
    barData = new int [numOfBar];
    barDes = new String[numOfBar];
    getBarData();
  }

  /********************************************************************************
   *                                                                              *
   *                               Functions                                      *
   *                                                                              *
   ********************************************************************************/

  public void getBarData()
  {
    try{
    String inputLine;
    int lineCount = -1;
    reader = createReader(barDataFile);
    while( (inputLine = reader.readLine()) != null  ) {
      String[] linePieces;
      lineCount ++;
      //reading lines
      if(inputLine != null && lineCount<numOfBar) {
        linePieces = split(inputLine, ",");
        //println("linePieces[1] is " +linePieces[1]);
        
        int value = Integer.parseInt(linePieces[0]);
        barDes[lineCount] = linePieces[1]+","+linePieces[2]+" - "+linePieces[3];
        //println(barDes[lineCount]);
        barData[lineCount] =value;
        if ( value> maxBar)
          maxBar = value;       
      }
    }
    }
    catch(IOException ioe){
      println("IOException" + ioe);
    }
  }
  public void render()
  {

    noFill();  
    strokeWeight(1);

    //Draw a frame,which doen`t change
    //rect(loTimeSlider.x1 - 4, loTimeSlider.y1 - 4, loTimeSlider.getWidth() + 8, loTimeSlider.getHeight() + 8);

    noStroke();
    //fill(255, 190, 0);
    fill(#19D9BE);

    float leftX=-1000;
    float rightX=1000;
    //Draw background graph    


    // Draw the bar Chart
    barHeight = 30;
    float singleHeight = 0 ;

    for( int i = 0;i<numOfBar;i++)
    {
      fill(179,226,227);
      stroke(0);
      strokeWeight(0.5);
      singleHeight = map( barData[i],0,maxBar,0,barHeight);
      rect(x1+i*ix,y1-singleHeight-5,ix,singleHeight+5);
      if( mouseX>=x1+i*ix && mouseX<x1+ix*(i+1)&& mouseY>=y1-singleHeight-5&&mouseY<y1 )
      {
          fill( 255,255,255);
          rect(x1+i*ix,y1-singleHeight-5,ix,singleHeight+5);
          fill(179,226,227,80);
          rect(x1+i*ix,y1-singleHeight-5,ix,singleHeight+5);
          cursor(HAND);
          drawBarDes(mouseX,mouseY,i);
      }
    }
    
    // Draw the slider 
    fill(#20805E);
    noStroke();
    rect(loTimeSlider.x1, loTimeSlider.y1, loTimeSlider.getWidth(), loTimeSlider.getHeight() );

    float scale = w / (2010 -1973);
    for ( int i = 1973 ;i<= 2010; i ++)
    {
      if( i %3 ==0)
      {
        fill(255);
        stroke(255);
        float x = x1+(i -1973)*scale;
        //println( "x is" +x);
        line(x,y1+2,x,y1+h-4 );
        textAlign(LEFT);
        String t = Integer.toString(i);

        float tWidth = textWidth(t);
        textSize(12);
        text(t,x-tWidth/2,y1+h+3,tWidth,30);

      }
    }
    // Draw the window selector handles
    drawHandle(loTimeSlider.x1+ left*ix, loTimeSlider.y1 + loTimeSlider.getHeight() / 2,handleWidth, handleHeight);
    drawHandle(loTimeSlider.x1+right*ix, loTimeSlider.y1 + loTimeSlider.getHeight() / 2,handleWidth, handleHeight);
  }
  
  void drawBarDes(float x, float y,int i)
  {
    float width= 160;
    float height = 40;
    stroke(178,183,187);
    strokeWeight(1);
    fill(255,255,253);
    rect(x-width/2,y-height-20,width,height);
    fill(0);
    textSize(10);
    textAlign(CENTER,CENTER);
    text("Time Span: "+barDes[i]+"\nNumber of Earthquake: "+ Float.toString(barData[i]),x-width/2,y-height-20,width,height);    
  }
  
  void setLeftDate( int le)
  {
    left = le;
  }
  void setRightDate(int ri)
  {
    right = ri;
  }

  public Date getLeftDate()
  {
    float currentDate = timeBegin + left *0.5;
    int mon = left%2==0?1:7;
    Date leftDate = new Date( int( floor(currentDate) ), mon   );
    return leftDate;
  }
  public Date getRightDate()
  {

    float currentDate = timeBegin + right *0.5;
    int mon = right%2==0?1:7;
    Date rightDate = new Date( int( floor(currentDate) ), mon   );
    return rightDate;
  }
  public void drawHandle(float x,float y,float w,float h)
  {
    image(handle, x-w/2,y-h/2-2,w,3.5*h/2);
  }

  public boolean mouseMoved() {
    if( mouseX>= x1 +left*ix - handleWidth/2 && mouseX<= x1 +left*ix+ handleWidth/2 && mouseY>=y1 &&mouseY<=y1+handleHeight ) {
      cursor(HAND);
      //drawHandleHelp(mouseX,mouseY);
      return true;
    }
    else if( mouseX>= x1 +right *ix - handleWidth/2 && mouseX<=x1 +right *ix+ handleWidth/2&& mouseY>=y1 &&mouseY<=y1+handleHeight ) {
      cursor(HAND);
      return true;
    }
    else {
      cursor(CROSS);
      return false;
    }
  }
  public void mouseClicked()
  {
    for( int i = 0;i<numOfBar;i++)
    {      
      if( mouseX>=x1+i*ix && mouseX<x1+ix*(i+1)&& mouseY>=y1-barHeight+10&&mouseY<y1 )
      {
          left = i;
          right = i+1;
      }
    }
  }
  public void mousePressed()
  {
    //Judge if left selector
    if( mouseX>= x1 +left*ix - handleWidth/2 && mouseX<= x1 +left*ix+ handleWidth/2 && mouseY>=y1 &&mouseY<=y1+handleHeight )
    {
      dragMode = 1;
      //println("Dragmode 1");
      return;
    }
    //Judge if right selector


    else if(mouseX>= x1 +right *ix- handleWidth/2 && mouseX<=x1 +right *ix+ handleWidth/2&& mouseY>=y1 &&mouseY<=y1+handleHeight)
    {
      //println("right is " + right + ", timeBegin is " + timeBegin );
      dragMode = 2;
      //println("Dragmode 2");
      return;
    }
  }


  public boolean mouseDragged()
  {
    //float ix = (handleBottom-handleTop)/9;
    if(dragMode != 0)
    {
      float curPos = 0;
      int dst = 1000;
      //float curY = handleTop;
      for(float i = 0; i < numOfBar; i++)
      {
        int cd = int(abs(mouseX - (x1+i*ix)));
        if(cd<dst)
        {
          curPos = i;
          dst = cd;
        }
        //curY += ix;
      }


      if(dragMode == 1)
      {

        left = curPos;
        //println("top is "+ top+", curPos is "+curPos);
        // if(top< 0 )
        //top= 0;
        if(left >= right) 
          left = right-1;
        //println("dst is "+dst+", curTime is "+curTime+",timeBegin is"+timeBegin);
      }
      else if(dragMode == 2)
      {
        //dayLength = curDay - dayStart + 1;
        right = curPos+1;
        //println("bottom is "+ bottom+", curPos is "+curPos);
        //println("right is "+ right);
        if(right>timeEnd)
          right =timeEnd;
        if(right <= left) 
          right = left+1;
        //println("dst is "+dst+", curTime is "+curTime+",timeEnd is"+timeEnd);
      }
      return true;
    }
    return false;
  }

  void mouseReleased()
  {
    dragMode = 0;
  }

  void drawHandleHelp ( float x, float y)
  {
    float width= 140;
    float height = 40;
    stroke(178,183,187);
    strokeWeight(1);
    fill(255,255,253);
    rect(x-width/2,y-height,width,height);
    fill(0);
    textSize(10);
    textAlign(CENTER,CENTER);
    text("Click and drag the sliders to filter earthquake by year",x-width/2,y-height,width,height);
  }
}

